package com.zjdiepu.www.proxy.biz.impl.transfer;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.proxy.biz.transfer.ITransferOtherAttachmentsService;
import com.zjdiepu.www.proxy.dao.transfer.TransferOtherAttachmentsMapper;
import com.zjdiepu.www.proxy.model.dto.transfer.TransferOtherAttachments;
import com.zjdiepu.www.proxy.model.ext.transfer.TransferOtherAttachmentsExt;

/**
 * 中转_other_附件信息Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年12月13日 17时50分<br/>
 * @since 1.0<br/>
 */
@Service
public class TransferOtherAttachmentsServiceImpl extends BaseService implements ITransferOtherAttachmentsService {
	public static final Logger logger = LoggerFactory.getLogger(TransferOtherAttachmentsServiceImpl.class);

	@Autowired
	private TransferOtherAttachmentsMapper transferOtherAttachmentsMapper;
	
	@Override
	public void saveUpdateAndDelete(TransferOtherAttachments transferOtherAttachments, OperateType operType) {
		switch (operType) {
		case add:
			transferOtherAttachments.initInsertData(transferOtherAttachments.getModifier(), transferOtherAttachments.getModifierName());
			transferOtherAttachmentsMapper.insert(transferOtherAttachments);
			break;
		case edit:
			transferOtherAttachments.initUpdateData(transferOtherAttachments.getModifier(), transferOtherAttachments.getModifierName());
			// 影响的记录数
			int updateCount = transferOtherAttachmentsMapper.updateByPrimaryKeySelective(transferOtherAttachments);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			transferOtherAttachments.initDeleteData(transferOtherAttachments.getModifier(), transferOtherAttachments.getModifierName());
			// 影响的记录数
			int deleteCount = transferOtherAttachmentsMapper.updateByPrimaryKeySelective(transferOtherAttachments);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<TransferOtherAttachments> selectByExt(TransferOtherAttachmentsExt transferOtherAttachmentsExt) {
		return transferOtherAttachmentsMapper.selectByExt(transferOtherAttachmentsExt);
	}
	
	@Override
	public int countByExt(TransferOtherAttachmentsExt transferOtherAttachmentsExt){
		return transferOtherAttachmentsMapper.countByExt(transferOtherAttachmentsExt);
	}
	
	@Override
	public TransferOtherAttachments selectById(String id) {
		return transferOtherAttachmentsMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			transferOtherAttachmentsMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(TransferOtherAttachments transferOtherAttachments) {
		return transferOtherAttachmentsMapper.countRefined(transferOtherAttachments);
	}

	@Override
	public TransferOtherAttachments selectRefined(TransferOtherAttachments transferOtherAttachments) {
		return transferOtherAttachmentsMapper.selectRefined(transferOtherAttachments);
	}
	
	@Override
	public int deletedRefined(TransferOtherAttachments transferOtherAttachments) {
		return transferOtherAttachmentsMapper.deletedRefined(transferOtherAttachments);
	}

	@Override
	public boolean repeat(TransferOtherAttachments transferOtherAttachments) {
		int count = countRefined(transferOtherAttachments);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(transferOtherAttachments.getId())) {
			return true;
		}
		TransferOtherAttachments dbItem = selectRefined(transferOtherAttachments);
		if(null == dbItem) {
			return false;
		}
		return !transferOtherAttachments.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return transferOtherAttachmentsMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<TransferOtherAttachments> transferOtherAttachmentss) {
		return transferOtherAttachmentsMapper.insertBatch(transferOtherAttachmentss);
	}
}
